package com.sgb.kjwl.viewmodel.home;

import android.text.TextUtils;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.repository.HomeRepertory;
import com.sgb.kjwl.view.ui.fragment.home.MerchantsFragment;
import com.sgb.link.entity.merchant.InvestmentProjectEntity;
import com.sgb.link.entity.merchant.TradeCategoryEntity;
import com.sgb.merchant.api.MerchantsNet;
import com.sgb.net.entity.ListResponse;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MerchantsFragModel extends BaseViewModel {

    private HomeRepertory repertory;
    private MerchantsFragment fragment;

    private Map map;
    private int pageNum = 1;

    public MerchantsFragModel(HomeRepertory repertory, MerchantsFragment fragment) {
        this.repertory = repertory;
        this.fragment = fragment;
    }

    /**
     * 行业一级分类列表
     */
    public void getindustryFirstCategoryList(int mItemType) {
//        if (fragment.getPosition() == 0 && !fragment.getIsRefresh()){
//            Type type = new TypeToken<List<TradeCategoryEntity>>() {
//            }.getType();
//            Object object =  DatalUtils.getInstance().getData(MkvUtils.CacheModular, CacheMarker.MERCHANTS_TYPE + mItemType, type);
//            if (object != null) {
//                List<TradeCategoryEntity> list = (List<TradeCategoryEntity>)object;
//                fragment.showCategoryTypeView(list);
//                return;
//            }
//        }
        MerchantsNet.getInstance().getService().getindustryFirstCategoryList().enqueue(new Callback<BaseEntity<List<TradeCategoryEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<TradeCategoryEntity>>> call, Response<BaseEntity<List<TradeCategoryEntity>>> response) {
                if (response.body() == null || response.body().getData() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    List<TradeCategoryEntity> dataList = response.body().getData();
//                    String cacheMateialistlHash = DatalUtils.getHashData(MkvUtils.CacheModular, CacheMarker.MERCHANTS_TYPE + mItemType);
//                    //获取从网络请求下来的数据的哈希值
//                    String mHashNet = DatalUtils.getInstance().strToHashKey(dataList);
//                    if (!cacheMateialistlHash.equals(mHashNet)) {
//                        DatalUtils.getInstance().putData(MkvUtils.CacheModular, CacheMarker.MERCHANTS_TYPE + mItemType, dataList);
//                    }
                    fragment.showCategoryTypeView(dataList);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<TradeCategoryEntity>>> call, Throwable t) {
            }
        });
    }

    public void getInvestmentData(int mItemType,String categoryFirst, boolean isLoadMore) {
        pageNum = isLoadMore ? pageNum + 1 : 1;
        map = new HashMap<>();
        map.put("page", pageNum);
        map.put("limit", 3);
        map.put("createTime", "desc");
        map.put("categoryFirst", categoryFirst);
        if (!(fragment.getPosition() == 0 && pageNum == 1 && TextUtils.isEmpty(categoryFirst))){
            DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        }
//        if (fragment.getPosition() == 0 && !fragment.getIsRefresh() && TextUtils.isEmpty(categoryFirst)){
//            Type type = new TypeToken<List<InvestmentProjectEntity>>() {
//            }.getType();
//            Object object =  DatalUtils.getInstance().getData(MkvUtils.CacheModular, CacheMarker.MERCHANTS_LIST + mItemType, type);
//            if (object != null) {
//                List<InvestmentProjectEntity> list = (List<InvestmentProjectEntity>)object;
//                fragment.showList(list,isLoadMore);
//                return;
//            }
//        }
        MerchantsNet.getInstance().getService().getInvestmentList(map).enqueue(new Callback<BaseEntity<ListResponse<List<InvestmentProjectEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<InvestmentProjectEntity>>>> call, Response<BaseEntity<ListResponse<List<InvestmentProjectEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null || response.body().getData().getList() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    List<InvestmentProjectEntity> dataList = response.body().getData().getList();
//                    String cacheMateialistlHash = DatalUtils.getHashData(MkvUtils.CacheModular, CacheMarker.MERCHANTS_TYPE + mItemType);
//                    //获取从网络请求下来的数据的哈希值
//                    String mHashNet = DatalUtils.getInstance().strToHashKey(dataList);
//                    if (!cacheMateialistlHash.equals(mHashNet)) {
//                        DatalUtils.getInstance().putData(MkvUtils.CacheModular, CacheMarker.MERCHANTS_TYPE + mItemType, dataList);
//                    }
                    fragment.showList(dataList, isLoadMore);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<InvestmentProjectEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

}
